/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiface.web.util;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.http.HttpSession;
import multiface.awwadco.jpa.Opzioni;
import multiface.awwadco.jpa.PersistenceManager;

/**
 *
 * @author Simona
 */
public class PagerList {

    private final static String CONT_QUERY = "count-query-page";
    private final static String ORDER = "order-page";
    private final static String DESC = "desc-page";
    private final static int MAX_PAGE_LIST= 20;

    public static void setContQuery(HttpSession s, Query q) {
        s.setAttribute(CONT_QUERY, q.getResultList().size());
    }

    public static int getContQuery(HttpSession s) {
        if (s.getAttribute(CONT_QUERY) == null) {
            return -1;
        } else {
            return Integer.parseInt(s.getAttribute(CONT_QUERY).toString());
        }
    }
    
    public static String outPagerList(HttpSession s, String functionJS, String jsp,int currentIndex, List<String> parameters, boolean admin) {
        int contQuery = getContQuery(s);
        if (contQuery == -1) {
            return "Error: List of pages is not possible because not executed before a PagerList.setContQuery()";
        }
        String st = "<div class=\"pager-list\">";
        int c = (contQuery / getMaxPage(admin)) + 1;
        int maxpag = getMaxPage(admin);
        for (int x = 0; x < c; x++) {
            if ((x * maxpag) == currentIndex) {
                st += "<a class=\"selected\" href=\"javascript:"+functionJS+"('" + jsp + "'," + convertParameterJSON(parameters) + ")\">" + (x + 1) + "</a>";
            } else {
                st += "<a href=\"javascript:"+functionJS+"('" + jsp + "'," + convertParameterJSON(parameters) + ")\">" + (x + 1) + "</a>";
            }
            if (x < c - 1) {
                st += " - ";
            }
        }
        return st + "</div>";
    }
    
    
    public static String outPagerList(HttpSession s, String jsp, int index, String attrs, boolean admin) {
        int contQuery = getContQuery(s);
        if (contQuery == -1) {
            return "Error: List of pages is not possible because not executed before a PagerList.setContQuery()";
        }
        String st = "<div class=\"pager-list\">";
        int c = (contQuery / getMaxPage(admin)) + 1;
        int maxpag = getMaxPage(admin);
        for (int x = 0; x < c; x++) {
            if ((x * maxpag) == index) {
                st += "<a class=\"selected\" href=\"javascript:open('" + jsp + "'," + getAttr(s, (x * maxpag),attrs) + ")\">" + (x + 1) + "</a>";
            } else {
                st += "<a href=\"javascript:open('" + jsp + "'," + getAttr(s, (x * maxpag),attrs) + ")\">" + (x + 1) + "</a>";
            }
            if (x < c - 1) {
                st += " - ";
            }
        }
        return st + "</div>";
    }

    public static String outPagerList(HttpSession s, String functionJS, String jsp, int index,boolean admin) {
        int contQuery = getContQuery(s);
        if (contQuery == -1) {
            return "Error: List of pages is not possible because not executed before a PagerList.setContQuery()";
        }
        String st = "<div class=\"pager-list\">";
        int c = (contQuery / getMaxPage(admin)) + 1;
        int maxpag = getMaxPage(admin);
        for (int x = 0; x < c; x++) {
            if ((x * maxpag) == index) {
                st += "<a class=\"selected\" href=\"javascript:"+functionJS+"('" + jsp + "'," + getAttr(s, (x * maxpag)) + ")\">" + (x + 1) + "</a>";
            } else {
                st += "<a href=\"javascript:"+functionJS+"('" + jsp + "'," + getAttr(s, (x * maxpag)) + ")\">" + (x + 1) + "</a>";
            }
            if (x < c - 1) {
                st += " - ";
            }
        }
        return st + "</div>";
    }
    public static String outPagerList(HttpSession s, String jsp, int index,boolean admin) {
        int contQuery = getContQuery(s);
        if (contQuery == -1) {
            return "Error: List of pages is not possible because not executed before a PagerList.setContQuery()";
        }
        String st = "<div class=\"pager-list\">";
        int c = (contQuery / getMaxPage(admin)) + 1;
        int maxpag = getMaxPage(admin);
        for (int x = 0; x < c; x++) {
            if ((x * maxpag) == index) {
                st += "<a class=\"selected\" href=\"javascript:open('" + jsp + "'," + getAttr(s, (x * maxpag)) + ")\">" + (x + 1) + "</a>";
            } else {
                st += "<a href=\"javascript:open('" + jsp + "'," + getAttr(s, (x * maxpag)) + ")\">" + (x + 1) + "</a>";
            }
            if (x < c - 1) {
                st += " - ";
            }
        }
        return st + "</div>";
    }

    public static int getMaxPage(boolean admin) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        //String pid = request.getParameter("pid");

        Opzioni op = em.createNamedQuery("Opzioni.findAll", Opzioni.class).getSingleResult();
        
        int max = op.getMaxRecordPag();
        if(admin){
            max= op.getMaxRecordPagAdmin();
        }
        
        em.close();
        //emf.close();
        
        return max;
    }

    public static void setOrder(HttpSession s, String order) {
        s.setAttribute(ORDER, order);
    }

    public static String getOrder(HttpSession s) {
        if (s.getAttribute(ORDER) == null) {
            return null;
        } else {
            return s.getAttribute(ORDER).toString();
        }
    }

    public static void setDesc(HttpSession s, String desc) {
        s.setAttribute(DESC, desc);
    }

    public static String getDesc(HttpSession s) {
        if (s.getAttribute(DESC) == null) {
            return null;
        } else {
            return s.getAttribute(DESC).toString();
        }
    }
    
    
    
    
    public static String convertParameterJSON(List<String> param,String addParam) {
        String ss = "{";
        
        for(int i=0;i<param.size();i++){
            ss+= param.get(i);
            if(i<param.size()-1){
                ss+=",";
            }
        }
        if(param.size()>0)ss+=",";
        ss+=addParam+"}";
        return ss;
    }

    public static String convertParameterJSON(List<String> param) {
        String ss = "{";
        for(int i=0;i<param.size();i++){
            ss+= param.get(i);
            if(i<param.size()-1){
                ss+=",";
            }
        }
        ss+="}";
        return ss;
    }
    
    private static String getAttr(HttpSession s, int index) {
        String ss = "{";
        if (getOrder(s) != null) {
            ss += "order:\'" + getOrder(s) + "\',";
        }
        if (getDesc(s) != null) {
            ss += "desc:\'" + getDesc(s) + "\',";
        }
        ss += "index:" + index + "}";
        return ss;
    }

    private static String getAttr(HttpSession s, int index, String attr) {
        String ss = "{";
        if (getOrder(s) != null) {
            ss += "order:\'" + getOrder(s) + "\',";
        }
        if (getDesc(s) != null) {
            ss += "desc:\'" + getDesc(s) + "\',";
        }
        ss+=attr;
        
        ss += "index:" + index + "}";
        return ss;
    }

    public static String getAttribute(HttpSession s, int index,boolean admin) {
        return getAttr(s, index * getMaxPage(admin));
    }
}
